package list;

/**
 * 约瑟夫问题原题：
 *  n个小孩子手拉手围成一个圈，编号为k(1 <= k <= n )的人从1开始报数，报到m的那个人出列，它的下一位又从1开始报数,
 *  报到m的又出列……依此类推，直到所有人都出列，由此产生一个出队编号的序列。
 *
 *  代码可以改进,弄懂后尝试改进
 */


public class JosephusDemo {
    public static void main(String[] args) {
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        //测试创建孩子环
        circleSingleLinkedList.addBoy(5);
        circleSingleLinkedList.showBoy();
        //测试出队
        circleSingleLinkedList.countBoy(10, 5,20);
    }
}

/**
 * 创建一个单向环形链表，first 指向头小孩，不变
 */
class CircleSingleLinkedList {
    private Boy first = null;

    //创建孩子环
    public void addBoy(int nums) {
        if(nums < 1) {
            System.out.println("nums invalid");
            return;
        }
        Boy curBoy = null;
        for(int i = 1; i <= nums; i++) {
            Boy boy = new Boy(i);
            if(i == 1) {
                first = boy;
                first.setNext(first);
                curBoy = first;
            } else {
              curBoy.setNext(boy);
              boy.setNext(first);
              curBoy = boy;
            }
        }
    }
    //遍历孩子
    public void showBoy() {
        if(first == null) {
            System.out.println("没有任何小孩~");
        }
        Boy curBoy = first;
        while (true) {
            System.out.printf("小孩的编号%d\n",curBoy.getNo());
            if(curBoy.getNext() == first) {
                break;
            }
            curBoy = curBoy.getNext();
        }
    }

    /**
     * 根据用户输入，计算小孩出圈顺序
     * @param startNo 编号为k
     * @param countNum 数到第m
     * @param nums 用于校验k是否正确
     */
    public void countBoy(int startNo, int countNum, int nums) {
        if(first == null || startNo < 1 || startNo > nums) {
            System.out.println("输入有误~");
            return;
        }
        Boy helper = first;
        while(true) {
            if(helper.getNext() == first) {    //将helper 指向 first 的前一节点
                break;
            }
            helper = helper.getNext();
        }
        for(int j = 0; j < startNo - 1; j++) {   //定位到编号为k的孩子
            first = first.getNext();
            helper = helper.getNext();
        }
        while(true) {
            if(helper == first) {  //循环出列，直到只剩一个孩子
                break;
            }
            for(int j = 0; j < countNum - 1; j++) {  //定位数到m的孩子
                first = first.getNext();
                helper = helper.getNext();
            }
            System.out.printf("小孩%d出圈\n",first.getNo());
            first = first.getNext();   //数到m的孩子出列
            helper.setNext(first);
        }
        System.out.printf("最后留在圈中的小孩是%d\n", first.getNo());
    }
}



class Boy {
    private int no;
    private Boy next;
    public Boy(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public void setNext(Boy next) {
        this.next = next;
    }

    public Boy getNext() {
        return next;
    }
}